package com.bjsdzk.collector.rpc;

import com.bjsdzk.collector.entity.telecom.TelecomToken;
import com.bjsdzk.collector.platform.coap.utils.ConstantSecond;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjsdzk.backend.rpc.XiYuService;
import com.bjsdzk.collector.serviceImp.TelecomSecondService;
import com.bjsdzk.collector.serviceImp.TelecomService;

import io.vertx.core.json.JsonObject;

@Service(
		version = "1.0.0",
        interfaceClass = XiYuService.class
)
@Component
public class  XiYuServiceimp implements XiYuService {

	private static Logger logger = LoggerFactory.getLogger(XiYuServiceimp.class);
    @Autowired
    private TelecomService telecomService;
    @Autowired
    private TelecomSecondService telecomSecondService;
    
	@Override
	public String deviceAdd(String dataContent) {
		logger.info("deviceAdd rpc :"+dataContent);
		 JsonObject json = new JsonObject(dataContent);
         String nodeId = json.getString("nodeId");
         String name = json.getString("name");
         String location = json.getString("location");
         String type = json.getString("type");
		 String deviceType = json.getString("deviceType");
         
		String verifyCode=nodeId;
		String data="";
		try {
			if("rk".equals(type)) {
				data=telecomSecondService.regDirectDevice(verifyCode,nodeId,name,location,deviceType);
			}else {
				data=telecomService.regDirectDevice(verifyCode,nodeId,name,location);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}


	@Override
	public void cmd(String msg) {
		logger.info("XiYuServiceimp cmd rpc :"+msg);
		JsonObject json = new JsonObject(msg);
		int fDetectorAddress = json.getInteger("fDetectorAddress");
		String cmd = json.getString("cmd");
		String deviceId = json.getString("deviceId");
		String type = json.getString("type");
		String serviceId = "";
		String method = "";
		int fDetectorType;
		int fCommandType;
		Integer fValue = null;
		if (StringUtils.equals(cmd, "rest")) {
			serviceId = "detectorMute";
			method = "SingleMute";
			fDetectorType = 4;
			fCommandType = 5;
			fValue = 0;
			try {
				telecomService.cmd(deviceId, serviceId, method, fDetectorAddress, fDetectorType, fCommandType, fValue);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			fValue=null;
			serviceId = "detectorReset";
			method = "SingleReset";
			fDetectorType = 4;
			fCommandType = 3;
			
		} else if (StringUtils.equals(cmd, "mute")) {
			serviceId = "detectorMute";
			method = "SingleMute";
			fDetectorType = 4;
			fCommandType = 5;
			fValue = json.getInteger("fValue");
		} else {
			serviceId = "detectorStatus";
			method = "QueryStatus";
			fDetectorType = 4;
			fCommandType = 1;

		}
		logger.debug("XiYuServiceimp cmd rpc :"+cmd);
		try {
			telecomService.cmd(deviceId, serviceId, method, fDetectorAddress, fDetectorType, fCommandType, fValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (StringUtils.equals(cmd, "rest")) {
			
		}

	}

	@Override
	public String data(String msg) {
		logger.info("data rpc :"+msg);
		JsonObject json = new JsonObject(msg);
		String deviceId=json.getString("deviceId");
		String gatewayId=json.getString("gatewayId");
		String pageNo=json.getString("pageNo");
		String pageSize=json.getString("pageSize");
		String startTime=json.getString("startTime");
		String endTime=json.getString("endTime");
		String type = json.getString("type");
		String deviceType = json.getString("deviceType");
		String data ="";
		try {
			if("rk".equals(type)) {
				data=telecomSecondService.queryDataHistory(deviceId, gatewayId, pageNo, pageSize, startTime, endTime,deviceType);
			}else {
				data=telecomService.queryDataHistory(deviceId, gatewayId, pageNo, pageSize, startTime, endTime);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}


	@Override
	public void deviceDelete(String msg) {
		logger.info("deviceDelete rpc :"+msg);
		JsonObject json = new JsonObject(msg);
		String type = json.getString("type");
		String deviceId=json.getString("deviceId");
		String deviceType = json.getString("deviceType");
		String data ="";
		try {
			if("rk".equals(type)) {
				data=telecomSecondService.deleteDirectDevice(deviceId,deviceType);
			}else {
				data=telecomService.deleteDirectDevice(deviceId);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void readStart(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readStop(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void errorStart(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void errorStop(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void readRestart(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void errorRestart(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ping(String msg) {
		// TODO Auto-generated method stub
		
	}
}